/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-11-04
 * Time: 15:45
 */
class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val){
        this.val = val;
    }
}

public class MyLinkedList {
    //连表的头引用
    public ListNode head;

    //遍历节点
    public void display(){
        ListNode cur = head;
        //cur走到最后一个节点的next是为空的
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        //判断head空不空
        if (head == null){
            head = node;
        }
        //先找到尾巴
        ListNode cur = head;
        //判断的是下一个节点是否为空
        while (cur.next != null){
            cur = cur.next;
        }
        //cur走到了最后一个节点
        cur.next = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        //判断index的合法性
        if (index < 0 || index > size()){
            System.out.println("index位置不合法!");
            return false;
        }
        ListNode node = new ListNode(data);
        ListNode cur = head;
        ListNode beforeCur = null;
        while (index > 0){
            //保存了index-1位置的节点
            beforeCur = cur;
            cur = cur.next;
            index--;
        }
        beforeCur.next = node;
        node.next = cur;
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if (head == null){
            return false;
        }
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        //先判断链表是否为空
        if (size() == 0){
            System.out.println("链表为空!");
            return;
        }
        //先判判断头节点
        if (head.val == key){
            head = head.next;
            return;
        }

        ListNode cur = head;
        ListNode perv;
        while (cur.next != null){
            perv = cur;
            cur = cur.next;
            if (perv.next.val == key){
                perv.next = cur.next;
                return;
            }
        }
        System.out.println("没有你要删除的关键字！");
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        //判断空不空
        if (this.head == null) {
            return;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while (cur != null){
            if (cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //最后处理头节点
        if (this.head.val == key){
            this.head = this.head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void clear(){
        this.head = null;
    }

}
